package sketch.ounit.fuzz;

import randoop.ExecutableSequence;
import sketch.util.Checker;

/**
	 * A class wrapping possible method argument. Given the passed object, an argument
	 * could either be the index or a new picked executable sequence
	 * */
class MethodArg {
		private int index = -1;
		
		private ExecutableSequence eseq = null;
		private int eseq_index = -1;
		
		public MethodArg(int index) {
//			Checker.checkTrue(index >= 0 && index < args.length, "The index is not valid: " + index
//					+ ", the object size is: " + args.length);
			this.index = index;
		}
		public MethodArg(ExecutableSequence eseq, int eseq_index) {
			Checker.checkNull(eseq, "The executable sequence could not be null.");
			this.eseq = eseq;
			this.eseq_index = eseq_index;
		}
		public boolean isPassedArg() {
			return index != -1;
		}
		public boolean isExecutableSequence() {
			return eseq != null;
		}
		public int getIndex() {
			return index;
		}
		public ExecutableSequence getESeq() {
			if(eseq != null) {
				Checker.checkTrue(eseq_index != -1, "The eseq index: " + eseq_index + " is not valid.");
			}
			return eseq;
		}
		public int getESeqIndex() {
			if(this.eseq_index != -1) {
				Checker.checkNull(eseq, "The executable sequence should not be null.");
			}
			return this.eseq_index;
		}
		public int size() {
			if(this.isPassedArg()) {
				return 1;
			} else {
				return this.eseq.sequence.size();
			}
		}
		@Override
		public int hashCode() {
			int code = 0;
			code += index*13;
			code += eseq_index*53;
			if(eseq != null) {
				code += 23 * eseq.hashCode();
			} else {
				code += 47;
			}
			return code;
		}
		@Override
		public boolean equals(Object obj) {
			if(!(obj instanceof MethodArg)) {
				return false;
			}
			MethodArg arg = (MethodArg)obj;
			return arg.index == this.index && (eseq != null ? (eseq.equals(arg.eseq)&& eseq_index == arg.eseq_index) : arg.eseq == null);
		}
		@Override
		public String toString() {
			if(index != -1) {
				return " obj index: " + index;
			} else {
				return " executable sequence with seq index: " + eseq_index;
			}
		}
	}